<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$branch_list = pdo_getall('yzzc_sun_branch', ['uniacid' => $_W['uniacid']], ['id', 'name']);
if ($_GPC['op'] == 'income') {
    $branch_id = $_GPC['id'];
    $range_time = $_GPC['range_time'] ?? [];
    $where = 'i.status= 0 and i.branch_id=' . $branch_id;
    $start_time = $end_time = '';
    if ($range_time) {
        $start_time = $range_time['start'];
        $end_time = $range_time['end'];
        if ($start_time && $end_time) {
            $end_time = strtotime($end_time);
            $start_time = strtotime($start_time);
            $where .= ' and i.createtime > ' . $start_time . ' and i.createtime<=' . $end_time;
        }
    }

    $branch_info = pdo_get('yzzc_sun_branch', ['uniacid' => $_W['uniacid'], 'id' => $branch_id], ['id', 'name']);
    $sql = 'SELECT i.id,i.total,i.type,i.platform_fee,i.branch_id,i.amount,i.status,i.createtime,o.ordernum,o.username,o.tel FROM ' . tablename('yzzc_sun_branch_income') . " i left join " . tablename('yzzc_sun_order') . " o on i.order_id=o.id where  {$where} ORDER BY i.createtime DESC";
    $info = pdo_fetchall($sql);
    $total = pdo_fetchcolumn("SELECT SUM(amount) as money FROM " . tablename("yzzc_sun_branch_income") . "where  branch_id = {$branch_id} and uniacid = " . $_W["uniacid"] . " and status = 0") ?? 0;
    $search_total = 0;
    if ($info) {
        foreach ($info as $key => $value) {
            foreach ($branch_list as $val) {
                if ($value['branch_id'] == $val['id']) {
                    $info[$key]['branch_name'] = $val['name'];
                }
            }
            $info[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
            if ($end_time && $start_time) {
                $search_total += $value['amount'];
            }
        }
    }
    include $this->template('web/branch_store_income');
    return;
}
if ($_GPC['op'] == 'branch_income') {
    $branch_id = $_GPC['branch_id'];
    $range_time = $_GPC['range_time'] ?? [];
    $where = 'status= 0 and branch_id=' . $branch_id;
    $type = $_GPC['type'];
    if ($type !== 'all') {
        $start_time = $_GPC['start_time'];
        $end_time = $_GPC['end_time'];
        $end_time = strtotime($end_time);
        $start_time = strtotime($start_time);
        $where .= ' and createtime > ' . $start_time . ' and createtime<=' . $end_time;
    }
    $branch_info = pdo_get('yzzc_sun_branch', ['uniacid' => $_W['uniacid'], 'id' => $branch_id], ['id', 'name', 'balance']);
    $sql = 'SELECT * FROM ' . tablename('yzzc_sun_branch_income') . " where {$where}";

    $info = pdo_fetchall($sql);
    if (!$info) {
        message('暂无结算收益', '', 'error');
    }
    $price = 0;
    foreach ($info as $value) {
        $price += $value['amount'];
    }

    $branch_update = pdo_update('yzzc_sun_branch', ['balance' => $branch_info['balance'] + $price], ['uniacid' => $_W['uniacid'], 'id' => $branch_id]);
    $update_sql = 'UPDATE ' . tablename('yzzc_sun_branch_income') . " set status= 1,income_time = " . time() . " where {$where}";
    $income_update = pdo_query($update_sql);
    if ($branch_update && $income_update) {
        message('结算成功', '', 'success');
    }
    message('结算失败，请联系管理员', '', 'error');
}
